import { Inject, Service } from "typedi";
import { SitesList } from "client/sitesVo";
import {
  repoGoodsInterface,
  REPO_GOODS_INTERFACE,
} from "infrastructure/rpc/goodsRepo";
import {
  REPO_PRODUCT_INTERFACE,
  repoProductInterface,
} from "infrastructure/rpc/productRepo";
import { RES_INFO, ERR_ALL_PROMISE } from "config/const";
import {
  repoSiteInterface,
  REPO_SITE_INTERFACE,
} from "infrastructure/rpc/siteRepo";
import {
  repoConfigInterface,
  REPO_CONFIG_INTERFACE,
} from "infrastructure/rpc/configRepo";
import { HttpParams } from "client/configVo";

import { ACCOUNT_INTERFACE, accountInterface } from "service/account/account";

export const GOODS_INTERFACE = "goodsInterface";

export interface goodsInterface {
  getList(opts: HttpParams): Promise<SitesList[]>;
  getOptions(opts: HttpParams): Promise<SitesList[]>;
  hidden(opts: HttpParams): Promise<SitesList[] | any>;
  delete(opts: HttpParams): Promise<SitesList[] | any>;
  get_sku(opts: HttpParams): Promise<SitesList[] | any>;
  update_white(opts: HttpParams): Promise<SitesList[] | any>;
  get_detail(opts: HttpParams): Promise<SitesList[] | any>;
}

export interface renderDataEntity {
  [key: string]: any | undefined;
  username: string;
  age: number;
}

@Service(GOODS_INTERFACE)
export class Goods implements goodsInterface {
  @Inject(REPO_GOODS_INTERFACE) private readonly goodsRepo: repoGoodsInterface;
  @Inject(REPO_PRODUCT_INTERFACE)
  private readonly productRepo: repoProductInterface;
  @Inject(REPO_CONFIG_INTERFACE)
  private readonly configRepo: repoConfigInterface;
  @Inject(REPO_SITE_INTERFACE) private readonly siteRepo: repoSiteInterface;
  @Inject(ACCOUNT_INTERFACE) private readonly accountInter: accountInterface;

  // 获取表格列表
  getList(opts: HttpParams): Promise<SitesList | any> {
    return new Promise(async (resolve, reject) => {
      const resList = await Promise.all([
        this.goodsRepo.getGoods(opts),
        this.goodsRepo.getTotal(opts),
      ]);
      const errRes = ERR_ALL_PROMISE(resList);
      if (errRes) return resolve(errRes);

      const [listInfo, totalInfo] = resList;

      // 列表为空，直接返回
      if (!listInfo?.data?.list?.length) {
        return resolve(
          RES_INFO({
            data: {
              listInfo: listInfo.data,
              totalInfo: totalInfo.data,
            },
          })
        );
      }

      // 通过id获取店铺列表
      const siteIds = listInfo?.data?.list?.map((item: any) => item.shop_id);
      const accountList: any = await Promise.all([
        this.accountInter.getList({
          ...opts,
          query: { page: 1, per_page: 9999, ids: [...new Set(siteIds)] },
        }),
      ]);
      const errAfterAccount = ERR_ALL_PROMISE(accountList);
      if (errAfterAccount) return resolve(errAfterAccount);
      const [accountInfo] = accountList;

      // 通过id获取商品列表
      const ids = listInfo?.data?.list?.map((item: any) => item.id);
      const accountIds = accountInfo?.data?.list?.map(
        (item: any) => item.account_id
      );
      const goodsList = await Promise.all([
        this.getListDetail({
          ...opts,
          query: [
            { name: "id", op: "IN", value: [...new Set(ids)].join(",") },
            {
              name: "account_id",
              op: "IN",
              value: [...new Set(accountIds)].join(","),
            },
          ],
        }),
      ]);
      const errAfterGoods = ERR_ALL_PROMISE(goodsList);
      if (errAfterGoods) return resolve(errAfterGoods);
      const [goodsInfo] = goodsList;

      // 整合数据
      const goodsObj: any = {};
      goodsInfo?.data?.list?.forEach((item: any) => (goodsObj[item.id] = item));
      const accountObj: any = {};
      accountInfo?.data?.list?.forEach(
        (item: any) => (accountObj[item.id] = item)
      );

      listInfo?.data?.list?.forEach((item: any) => {
        item.detail = goodsObj[item.id];
        item.accountInfo = accountObj[item.shop_id];
      });
      resolve(
        RES_INFO({
          data: {
            listInfo: listInfo.data,
            totalInfo: totalInfo.data,
          },
        })
      );
    });
  }

  // 通过查询条件,去订单中心获取商品列表详情
  getListDetail(opts: HttpParams): Promise<SitesList | any> {
    return this.productRepo.getGoodsByIds(opts);
  }

  // 获取下拉数据
  getOptions(opts: HttpParams): Promise<SitesList[] | any> {
    const { request, response, userInfo } = opts;
    return new Promise(async (resolve, reject) => {
      const resList = await Promise.all([this.configRepo.getOrgan(opts)]);
      const errRes = ERR_ALL_PROMISE(resList);
      if (errRes) return resolve(errRes);
      const [organRes] = resList;
      resolve(
        RES_INFO({
          data: {
            organList: organRes?.data || [],
          },
        })
      );
    });
  }

  // 批量隐藏
  hidden(opts: HttpParams) {
    return this.goodsRepo.hidden(opts);
  }

  // 批量下架
  delete(opts: HttpParams) {
    return this.goodsRepo.delete(opts);
  }

  // 获取sku信息
  get_sku(opts: HttpParams) {
    return this.goodsRepo.get_sku(opts);
  }

  // 批量更新产品是否在白名单
  update_white(opts: HttpParams) {
    return this.goodsRepo.update_white(opts);
  }

  // 查询商品详情
  get_detail(opts: HttpParams) {
    return this.productRepo.get_detail(opts);
  }
}
